﻿/*
	Copyright 2010 Intuitive Solutions

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

	   http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/

using System;
using System.Collections.Generic;
using Crochet.Configuration;
using Crochet.Core.Policy;

namespace Crochet.Core
{
	public class SchedulerFactory
	{
		private static SchedulerFactory sfInstance = null;

		protected SchedulerFactory()
		{
		}

		public static SchedulerFactory Instance
		{
			get
			{
				if (sfInstance == null)
				{
					sfInstance = new SchedulerFactory();
				}

				return sfInstance;
			}
		}

		public IScheduler CreateScheduler(SchedulerType stSchedulerType, string sName)
		{
			return CreateScheduler(stSchedulerType, GetParams(stSchedulerType, sName));
		}

		public IScheduler CreateScheduler(SchedulerType stSchedulerType, params object[] oaParams)
		{
			IScheduler sScheduler = null;

			IExecutionPolicy epExecutionPolicy = ExecutionPolicyFactory.Instance.Create(oaParams != null ? (string)oaParams[1] : string.Empty, null, null);

			switch (stSchedulerType)
			{
				case SchedulerType.TimerPool:
					sScheduler = new TimerPoolScheduler((int)oaParams[2], (bool)oaParams[3], (int)oaParams[4], epExecutionPolicy);
					break;
				case SchedulerType.Immediate:
					sScheduler = new ImmediateScheduler(epExecutionPolicy);
					break;
				case SchedulerType.ThreadPool:
					sScheduler = new ThreadPoolScheduler((int)oaParams[2], epExecutionPolicy);
					break;
				case SchedulerType.Sequential:
					sScheduler = new SequentialScheduler(epExecutionPolicy);
					break;
				case SchedulerType.ExecutionPool:
					sScheduler = new ExecutionPoolScheduler((int)oaParams[2], epExecutionPolicy);
					break;
				default:
					break;
			}

			return sScheduler;
		}

		public object[] GetParams(SchedulerType stSchedulerType, string sName)
		{
			object[] oParams = null;

			switch (stSchedulerType)
			{
				case SchedulerType.TimerPool:
					oParams = CrochetConfiguration.Instance.TimerSchedulers.GetAttributes(sName);
					break;
				case SchedulerType.Immediate:
					oParams = CrochetConfiguration.Instance.ImmediateSchedulers.GetAttributes(sName);
					break;
				case SchedulerType.ThreadPool:
					oParams = CrochetConfiguration.Instance.ThreadPoolSchedulers.GetAttributes(sName);
					break;
				case SchedulerType.ExecutionPool:
					oParams = CrochetConfiguration.Instance.ExecutionPoolSchedulers.GetAttributes(sName);
					break;
				default:
					break;
			}

			return oParams;
		}
	}
}
